package cn.crm.system.service.impl;

import cn.crm.common.annotation.DataScope;
import cn.crm.common.utils.SecurityUtils;
import cn.crm.system.domain.pojo.dto.*;
import cn.crm.system.domain.pojo.entity.Business;
import cn.crm.system.domain.pojo.vo.BusinessVo;
import cn.crm.system.domain.pojo.vo.QuotedPriceVo;
import cn.crm.system.domain.pojo.vo.QuotedPricebuildVo;
import cn.crm.system.mapper.*;
import cn.crm.system.service.IBusinessService;
import cn.crm.system.service.IOpemSeroesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 处理客户的接口
 */

@Slf4j
@Service
public class OpemSeroeslmpl implements IOpemSeroesService {

    @Autowired
    private QuotedPriceMapper quotedPriceMapper;
    @Autowired
    private QuotedPricebuildMapper quotedPricebuildMapper;
    @Autowired
    private PricePdfMapper   pricePdfMapper;
//    新增报价单
    @Override
    public Long insertquotedPrice(QuotedPriceDto payload) {
        log.debug("开始新增报价单");
        int i = quotedPriceMapper.insertquotedPrice(payload);
        List<QuotedPricebuildDto> quotedPricebuildDtos = payload.getQuotedPricebuildDtos();
        Map<String, Object> params = new HashMap<>();
        params.put("quotedPricebuildDtos", quotedPricebuildDtos);
        params.put("priceId", payload.getId());
        int i1 = quotedPricebuildMapper.insertquotedPricebuild(params);
        return payload.getId();
    }
//查出生成报价单
    @Override
    public QuotedPriceVo SelectquotedPrice(Long id) {
        log.debug("开始查询出打印的东西");
        QuotedPriceVo quotedPriceVo = quotedPriceMapper.SelectquotedPrice(id);
        //然后开是查表身
        List<QuotedPricebuildVo> quotedPricebuildVos = quotedPricebuildMapper.SelectquotedPricebuild(id);
        quotedPriceVo.setQuotedPricebuildDtos(quotedPricebuildVos);
        return quotedPriceVo;
    }
//添加报价单到PDF数据库
    @Override
    public Long insertPDF(PricePdfDto pricePdfDto) {
        log.debug("开始添加报价单到PDF数据库");
        Long aLong = pricePdfMapper.insertPDF(pricePdfDto);
        return aLong;
    }
    //修改报价单
    @Override
    public int updatequotedPrice(QuotedPriceDto payload) {
        //开始修改报价单
        int i = quotedPriceMapper.updatequotedPrice(payload);
        //然后开始修改报价单资料
        List<QuotedPricebuildDto> quotedPricebuildDtos = payload.getQuotedPricebuildDtos();
        Map<String, Object> params = new HashMap<>();
        params.put("quotedPricebuildDtos", quotedPricebuildDtos);
        params.put("priceId", payload.getId());
        int i1 = quotedPricebuildMapper.delquotedPricebuild(new Long[]{payload.getId()});
        int i2 = quotedPricebuildMapper.insertquotedPricebuild(params);
        return i+i1+i2;
    }

    @Override
    public int deleteitem(Long id) {
        log.debug("开始删除报价单");
        int i = quotedPriceMapper.deleteitem(id);
        if (i == 0){
            return 0;
        }
        return i;

    }
}
